import pinyin from 'js-pinyin'
import { familyTree } from '@/utils/slp'
pinyin.setOptions({ checkPolyphone: false, charCase: 0 })

export const baseData = {
  uploadSetting: {
    type: 'upload',
    label: '文档',
    prop: 'file',
    value: '',
    action: import.meta.env.VITE_APP_BASE_API + '/file/upload',
    fileList: [],
    uploadType: [
      {
        type: 'application/vnd.ms-powerpoint',
        size: 50,
        ext: 'ppt',
        cate: 'office',
      },
      {
        type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
        size: 50,
        ext: 'pptx',
        cate: 'office',
      },
      {
        type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
        size: 10,
        ext: 'xlsx',
        cate: 'office',
      },
      {
        type: 'application/vnd.ms-excel',
        size: 10,
        ext: 'xls',
        cate: 'office',
      },
      {
        type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
        size: 20,
        ext: 'docx',
        cate: 'office',
      },
      {
        type: 'application/msword',
        size: 20,
        ext: 'doc',
        cate: 'office',
      },
      {
        type: 'text/plain',
        size: 10,
        ext: 'txt',
        cate: 'office',
      },
      {
        type: 'image/jpeg',
        size: 50,
        ext: 'jpeg',
        cate: 'image',
      },
      {
        type: 'image/jpg',
        size: 50,
        ext: 'jpg',
        cate: 'image',
      },
      {
        type: 'image/bmp',
        size: 50,
        ext: 'bmp',
        cate: 'image',
      },
      {
        type: 'image/png',
        size: 50,
        ext: 'png',
        cate: 'image',
      },
      {
        type: 'image/gif',
        size: 50,
        ext: 'pgifpt',
        cate: 'image',
      },
      {
        type: 'image/DNG',
        ext: 'DNG',
        cate: 'image',
      },
      {
        type: 'image/tiff',
        size: 50,
        ext: 'tiff',
        cate: 'image',
      },
      {
        type: 'image/CR2',
        size: 50,
        ext: 'CR2',
        cate: 'image',
      },
      {
        type: 'image/NEF',
        size: 50,
        ext: 'NEF',
        cate: 'image',
      },
      {
        type: 'application/pdf',
        size: 20,
        ext: 'pdf',
        cate: 'pdf',
      },
      {
        type: 'application/zip',
        size: 1024,
        ext: 'zip',
        cate: 'file',
      },
      {
        type: 'application/x-zip-compressed',
        size: 1024,
        ext: 'zip',
        cate: 'file',
      },
      {
        type: 'zip',
        size: 1024,
        ext: 'zip',
        cate: 'file',
      },
      {
        type: 'rar',
        size: 1024,
        ext: 'rar',
        cate: 'file',
      },
      {
        type: 'video/mp4',
        size: 2048,
        ext: 'mp4',
        cate: 'video',
      },
      {
        type: 'video/avi',
        size: 2048,
        ext: 'avi',
        cate: 'video',
      },
      {
        type: 'video/x-ms-wmv',
        size: 2048,
        ext: 'wmv',
        cate: 'video',
      },
      {
        type: 'rm',
        size: 2048,
        ext: 'rm',
        cate: 'video',
      },
      {
        type: 'rmvb',
        size: 2048,
        ext: 'rmvb',
        cate: 'video',
      },
      {
        type: 'video/quicktime',
        size: 2048,
        ext: 'mov',
        cate: 'video',
      },
      {
        type: 'flv',
        size: 2048,
        ext: 'flv',
        cate: 'video',
      },
      {
        type: 'video/3gpp',
        size: 2048,
        ext: '3gp',
        cate: 'video',
      },
      {
        type: 'video/x-matroska',
        size: 2048,
        ext: 'mkv',
        cate: 'video',
      },
    ],
    bottomTip: '支持：ppt，excel，word，文本txt，图片，PDF，压缩包(zip，rar)',
    bottomTipVideo: '视频(mp4、avi、wmv、rm、rmvb、mov、flv、3gp、mkv)',
    span: 24,
    noAutoUpload: true,
  },
}

// 获取分类父级拼音
/* export function getFullNamePinYin(id, fileCate, deptName) {
  let arr = [...new Set(familyTree(fileCate, id))]
  let deptNamePinyin = arr.map((element) => {
    return element.categoryName
  })
  deptNamePinyin = deptNamePinyin.reverse()
  deptNamePinyin.unshift(deptName)
  let all = deptNamePinyin.map((item) => {
    return pinyin.getCamelChars(item)
  })
  return all.join('-')
} */

// 获取分类父级codeRule
export function getFullNamePinYin(id, fileCate, deptName) {
  let arr = [...new Set(familyTree(fileCate, id))]
  let deptNamePinyin = arr.map((element) => {
    return element.codeRule
  })
  deptNamePinyin = deptNamePinyin.reverse()
  if (deptName) {
    deptNamePinyin.unshift(pinyin.getCamelChars(deptName))
  }
  return deptNamePinyin.join('-')
}

export function getFileName(fileName) {
  if (!fileName) {
    return ''
  }
  //获取最后一个.的位置
  let index = fileName.lastIndexOf('.')
  //获取后缀
  return fileName.substr(index + 1)
}
export function getFileType(path) {
  let obj = getFileBaseObj(path)
  return obj.type
}
export function getFileCate(path) {
  let obj = getFileBaseObj(path)
  return obj.cate
}
/**
 *   {
        type: 'application/vnd.ms-powerpoint',
        size: 50,
        ext: 'ppt',
        cate: 'office',
      },
 */
export function getFileBaseObj(path) {
  const ext = getFileName(path)
  const fileObj = baseData.uploadSetting.uploadType.find((element) => {
    return element.ext === ext || element.type.indexOf(ext) != -1
  })
  return fileObj
}
// 返回所有类型
export function getAllType(files) {
  return files.map((element) => {
    return element.type
  })
}

// 获取当前上传文档限制配置
export function getCurrentFile(files, target) {
  return files.filter((element) => {
    return element.type === target
  })
}

// 用于控制文档的大小和类型
export function checkFile(file, item, proxy) {
  const allType = getAllType(item.uploadType)
  // 文档类别或者文档后缀
  const fileTypeOrExt = file.type || getFileName(file.name)
  if (allType && allType.length > 0) {
    // 判断文档类别 或者 文档名后缀是否是符合格式
    const isType = allType.includes(fileTypeOrExt)
    if (!isType) {
      proxy.$message.error('不支持该格式文档，请重新上传')
      return false
    }
  }

  // 当前上传文档限制
  const currentFile = getCurrentFile(item.uploadType, fileTypeOrExt)
  if (currentFile[0].size) {
    const isFileSize = file.size / 1024 / 1024 < currentFile[0].size
    if (!isFileSize) {
      proxy.$message.error('最大上传大小为' + currentFile[0].size + 'M')
      return false
    }
  }
  return true
}

// 提交表单前进行校验
export async function submitForm(proxy) {
  let result = await proxy.$refs['formRef'].validate((valid) => {
    if (valid) {
      return true
    } else {
      return false
    }
  })
  return result
}

// 现在只能输入英文和数字
export function accountInput(val) {
  let codeReg = new RegExp('[A-Za-z0-9.]+'), //正则 英文+数字；
    len = val.length,
    str = ''
  for (let i = 0; i < len; i++) {
    if (codeReg.test(val[i])) {
      str += val[i]
    }
  }
  return str
}
